package com.shujiaotong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shujiaotong.mapper.OrderDetailMapper;
import com.shujiaotong.pojo.OrderDetail;
import com.shujiaotong.pojo.Orders;
import com.shujiaotong.mapper.OrdersMapper;
import com.shujiaotong.service.IOrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-10-20
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersService {


    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Override
    public Page<Orders> getOrdersWithDetails(Integer page, Integer pageSize, Integer status) {
        // 创建分页条件
        Page<Orders> orderPage = new Page<>(page, pageSize);
        // 创建查询条件
        LambdaQueryWrapper<Orders> oqw = new LambdaQueryWrapper<>();
        if (status != null) {
            oqw.eq(Orders::getStatus, status);
        }
        this.page(orderPage, oqw);
        List<Orders> ordersList = orderPage.getRecords();

        // 通过订单 ID 查询订单详情并填充到 Orders 对象中
        ordersList.forEach(order -> fillOrderDetails(order));

        return orderPage;
    }

    /**
     * 通过订单 ID 查询订单详情并填充到 Orders 对象中
     */
    private void fillOrderDetails(Orders order) {
        OrderDetail detail = orderDetailMapper.selectById(order.getOrderId());
        if (detail != null) {
            order.setCreateTime(detail.getCreateTime());
            order.setEndTime(detail.getEndTime());
            order.setDescription(detail.getDescription());
        }
    }
}
